package br.poli.mesaterapeutica.WWMesaTerapeutica;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.jdesktop.application.Application;
import org.uweschmidt.wiimote.whiteboard.WiimoteDataHandler;
import org.uweschmidt.wiimote.whiteboard.WiimoteWhiteboard;
import org.uweschmidt.wiimote.whiteboard.calibration.CalibrationPersistence;
import org.uweschmidt.wiimote.whiteboard.calibration.WiimoteCalibration;
import org.uweschmidt.wiimote.whiteboard.gui.AboutWindow;
import org.uweschmidt.wiimote.whiteboard.gui.HelpHandler;
import org.uweschmidt.wiimote.whiteboard.gui.LogWindow;
import org.uweschmidt.wiimote.whiteboard.gui.MainPanel;
import org.uweschmidt.wiimote.whiteboard.gui.MenuBar;
import org.uweschmidt.wiimote.whiteboard.gui.PreferencesWindow;
import org.uweschmidt.wiimote.whiteboard.preferences.WWPreferences;
import org.uweschmidt.wiimote.whiteboard.tuio.TuioTransmitter;
import org.uweschmidt.wiimote.whiteboard.util.UpdateNotifier;
import org.uweschmidt.wiimote.whiteboard.util.Util;
import org.uweschmidt.wiimote.whiteboard.util.WiiRemoteJErrorHandler;

import apple.dts.samplecode.osxadapter.OSXAdapter;

import wiiremotej.WiiRemoteJ;

public class WWMesaTerapeutica extends org.uweschmidt.wiimote.whiteboard.WiimoteWhiteboard{
	
	
	public static void main(String args[]) {
		if (Util.MAC_OS_X && !Util.INSIDE_APP_BUNDLE) {
			System.setProperty("apple.laf.useScreenMenuBar", "true");
			System.setProperty("com.apple.mrj.application.apple.menu.about.name", getProperty("id"));
		}
		Application.launch(WWMesaTerapeutica.class, args);
	}
	
	@Override
	protected void startup() {		
		try {
			// blocking
			if (WWPreferences.getPreferences().checkForUpdates())
				UpdateNotifier.checkForUpdate(getProperty("version"));
			
			Logger.getLogger("wiimotewhiteboard").setUseParentHandlers(false);

			final JFrame f = getMainFrame();
			
			WiimoteCalibration calibration = new WiimoteCalibration();
			WiimoteDataHandler dh = new WWMTDataHandler(calibration);
			
			LogWindow lw = new LogWindow();
			MainPanel mp = new MainPanel(dh, calibration);		
			AboutWindow af = new AboutWindow();
			HelpHandler hh = new HelpHandler();
			PreferencesWindow pf = new PreferencesWindow(mp, hh);
			f.setJMenuBar(new MenuBar(pf, af, hh, lw));
			registerForMacOSXEvents(pf, af);

			// add persistence as last listener because it indirectly generates new calibration events
			new CalibrationPersistence(calibration);
			// TODO save last used screen?
			calibration.setScreen(WiimoteCalibration.DEFAULT_SCREEN);
			
			new TuioTransmitter(dh, calibration);
			
			WiiRemoteJ.setConsoleLoggingErrors();
			Logger.getLogger("wiiremotej").setLevel(Level.ALL);
			Logger.getLogger("wiiremotej").addHandler(new WiiRemoteJErrorHandler(dh));
			
			try {
				// restore session manually before 'show' on the main panel is
				// called. this is a fix to the problem that the session is not
				// restored; apparently because mainFrame is not resizable
				getContext().getSessionStorage().restore(f, "mainFrame.session.xml");
			} catch (Exception e) {}
			
			show(mp);
			// f.pack() is called in 'show' above if f.isValid() == false
			f.pack();
			
		} catch (Exception e) {
			e.printStackTrace();
			getLogger().log(Level.SEVERE, Util.getResourceMap(WiimoteWhiteboard.class).getString("startupError"), e);
			JOptionPane.showMessageDialog(null, e.getMessage(), getProperty("id"), JOptionPane.ERROR_MESSAGE);
			exit();
		}
	}
	
	private void registerForMacOSXEvents(PreferencesWindow pf, AboutWindow af) {
		if (Util.MAC_OS_X) {
	        try {
				OSXAdapter.setQuitHandler(this, WiimoteWhiteboard.class.getDeclaredMethod("quitApp", (Class[])null));				
				if (!Util.INSIDE_APP_BUNDLE) {
					OSXAdapter.setAboutHandler(af, AboutWindow.class.getDeclaredMethod("about", (Class[])null));
				}
				OSXAdapter.setPreferencesHandler(pf, PreferencesWindow.class.getDeclaredMethod("preferences", (Class[])null));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
